/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 2; tab-width: 8 -*- */
/*
 * gtrshortenurl.c
 * Copyright (C) Jonas Hagmar 2009 <gnotterdev@gmail.com>
 * 
 * gtrshortenurl.c is free software: you can redistribute it and/or modify it
 * under the terms of the GNU General Public License as published by the
 * Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * gtrshortenurl.c is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License along
 * with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
#include <malloc.h>
#include "gtrshortenurl.h"
#include "twitter_api.h"

typedef struct
{
  GCond *cond;
  GMutex *mutex;
  gchar *url;
  gboolean done;
  char *short_url;
  GThread *thread;
} ShortenUrlCxt;

static gpointer
get_short_url (gpointer data)
{
  ShortenUrlCxt *cxt = (ShortenUrlCxt *) data;
  char *short_url;

  short_url = twapi_shorten_url (cxt->url);

  g_mutex_lock (cxt->mutex);
  cxt->short_url = short_url;
  cxt->done = TRUE;
  g_cond_signal (cxt->cond);
  g_mutex_unlock (cxt->mutex);
  return NULL;
}

gint
gtr_shorten_url (gchar const *url, GtkWindow * parent, gchar **short_url)
{
  GtkWidget *dialog;
  GtkWidget *content_area;
  GtkWidget *label;
  GtkWidget *entry;
  ShortenUrlCxt cxt;
  GTimeVal wt1, wt2;
  GtkWidget *p_bar;
  gint rv;

  if (!g_thread_supported ())
    g_thread_init (NULL);

  *short_url = NULL;

  dialog = gtk_dialog_new_with_buttons ("Shorten URL",
					parent,
					GTK_DIALOG_MODAL |
					GTK_DIALOG_DESTROY_WITH_PARENT,
					GTK_STOCK_OK, GTK_RESPONSE_OK,
					GTK_STOCK_CANCEL, GTK_RESPONSE_CANCEL,
					NULL);

  content_area = gtk_dialog_get_content_area (GTK_DIALOG (dialog));

  label = gtk_label_new_with_mnemonic ("_URL to shorten:");
  gtk_label_set_use_markup (GTK_LABEL (label), TRUE);
  gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
  gtk_misc_set_alignment (GTK_MISC (label), 0, 0.5);

  gtk_box_pack_start (GTK_BOX (content_area), label, FALSE, FALSE, 6);

  entry = gtk_entry_new ();

  if (url != NULL)
    {
      gtk_entry_set_text (GTK_ENTRY (entry), url);
    }

  gtk_box_pack_start (GTK_BOX (content_area), entry, TRUE, TRUE, 6);
  gtk_label_set_mnemonic_widget (GTK_LABEL (label), entry);

  gtk_widget_show_all (content_area);

  rv = gtk_dialog_run (GTK_DIALOG (dialog));

  if (rv == GTK_RESPONSE_OK)
    {
      gtk_widget_set_sensitive (dialog, FALSE);

      while (gtk_events_pending ())
	gtk_main_iteration ();

      cxt.mutex = g_mutex_new ();
      cxt.cond = g_cond_new ();
      cxt.url = g_strdup (gtk_entry_get_text (GTK_ENTRY (entry)));
      cxt.short_url = NULL;
      cxt.done = FALSE;
      g_get_current_time (&wt1);
      g_time_val_add (&wt1, 500000);
      g_mutex_lock (cxt.mutex);
      cxt.thread =
	g_thread_create (get_short_url, (gpointer) & cxt, TRUE, NULL);
      if (!g_cond_timed_wait (cxt.cond, cxt.mutex, &wt1))
	{

	  gtk_label_set_text (GTK_LABEL (label), "Shortening URL...");

	  gtk_widget_destroy (entry);

	  p_bar = gtk_progress_bar_new ();

	  gtk_box_pack_start (GTK_BOX (content_area), p_bar, TRUE, TRUE, 6);

	  gtk_widget_show_all (content_area);

	  while (gtk_events_pending ())
	    gtk_main_iteration ();

	  while (cxt.done == FALSE)
	    {
	      gtk_progress_bar_pulse (GTK_PROGRESS_BAR (p_bar));

	      while (gtk_events_pending ())
		gtk_main_iteration ();

	      g_get_current_time (&wt2);
	      g_time_val_add (&wt2, 100000);
	      g_cond_timed_wait (cxt.cond, cxt.mutex, &wt2);
	    }

	}
      g_thread_join (cxt.thread);
      g_mutex_unlock (cxt.mutex);
      g_mutex_free (cxt.mutex);
      g_cond_free (cxt.cond);
      g_free (cxt.url);
      if (cxt.short_url != NULL)
	{
	  *short_url = g_strdup (cxt.short_url);

	  free (cxt.short_url);
	}
    }

  gtk_widget_destroy (dialog);

  return rv;
}
